//
//  WXEventModule.swift
//  Scofield
//
//  Created by eme on 2017/2/14.
//  Copyright © 2017年 eme. All rights reserved.
//

import Foundation
import URLNavigator
import UtilCore
import ModelProtocol
import WeexSDK
import NetWorkCore

class WXEventModule : NSObject, WXEventModuleProtocol{
    public var weexInstance:WXSDKInstance?;
    public static func wx_export_method_openURL() -> String{
        return NSStringFromSelector(#selector(self.openURL(_:)))
    }
    public static func wx_export_method_openNative() -> String{
        return NSStringFromSelector(#selector(self.openNative(_:)))
    }
    static func wx_export_method_requestModel() -> String{
        return NSStringFromSelector(#selector(self.requestModel(_: callback:)))
    }
    static func wx_export_method_showLoading() -> String{
        return NSStringFromSelector(#selector(self.showLoading(_:)))
    }
    static func wx_export_method_showMsg() -> String{
        return NSStringFromSelector(#selector(self.showMsg(_: callback:)))
    }
    static func wx_export_method_scanQrCode() -> String{
        return NSStringFromSelector(#selector(self.scanQrCode(_: callback:)))
    }
    static func wx_export_method_getAuthInfo() -> String{
        return NSStringFromSelector(#selector(self.getAuthInfo(_:)))
    }
    func openURL(_ url: String!) {
        var newURL:String! = url;
        if (url.hasPrefix("//")) {
            newURL = String.localizedStringWithFormat("http:%@", url);
        } else if (!url.hasPrefix("http")) {
            newURL = NSURL.init(string: url, relativeTo: weexInstance?.scriptURL)?.absoluteString
        }
        NSLog("==url:%@", newURL)
        let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
        let mainVc = storyboard.instantiateViewController(withIdentifier: "WeeX_Vc") as! WeeX_Vc
        mainVc.url = newURL
        weexInstance?.viewController.navigationController?.pushViewController(mainVc, animated: true);
    }
    func openNative(_ url: String!) {
        _ = Navigator.open(url)
    }
    func requestModel(_ model: String!, callback: WXModuleCallback!) {
        NSLog("==model:%@", model)
        if(model == "camera"){
            if((callback) != nil){
                callback(["result":"WX_FAILED","message":"暂不支持"])
            }
        }
    }
    func showLoading(_ isShow: String!) {
        if let fromVc = UIViewController.topMost {
            if isShow == "1"{
                fromVc.view.showLoading()
            }else{
                fromVc.view.hideLoading()
            }
        }
        
    }
    func showMsg(_ message: String!, callback: WXModuleCallback!) {
        if let fromVc = UIViewController.topMost {
            fromVc.view.toastCompletion(message, position: .center){ _ in
                if((callback) != nil){
                    callback(["result":"WX_FAILED","message":"暂不支持"])
                }
            }
        }
    }
    ///二维码扫描
    func scanQrCode(_ message: String!, callback: WXModuleCallback!) {
        if let fromVc = UIViewController.topMost {
            let scanVc = UtilCore.scanVc
            scanVc.didScanValue_Block = { relValue in
                callback(["relValue":relValue])
               _ = fromVc.navigationController?.popViewController(animated: true)
            }
            fromVc.navigationController?.pushViewController(scanVc, animated: true)
        }
    }
    ///获取用户信息
    func getAuthInfo(_ callback: WXModuleCallback!) -> Void {
        if let user = LoginedUser.sharedInstance.user{
            let param = ["user":user.dictionaryRepresentation(),"headers":NetWorkAPI.sharedInstance.headers as [String : Any]]
            print(param)
            callback(param)
        }
        
    }
}
